package com.hae.mail.order.micomailorderservice.controller;

import com.hae.mail.order.api.OrderService;
import com.hae.mail.order.micomailorderservice.feign.UserFallBackFeign;
import com.hae.mail.order.micomailorderservice.feign.UserFeign;
import com.hae.mail.order.model.OrderDao;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping
public class OrderServiceImpController implements OrderService {

    @Autowired
    private UserFeign userFeign;

    @Autowired
    private UserFallBackFeign userFallBackFeign;

    /**
     * @param userName
     * @return
     */
    @RequestMapping("/getOrdersByUserName")
    @Override
    public OrderDao getOrdersByUserName(@RequestParam("userName") String userName) {
        OrderDao orderDao = new OrderDao();
        String user = userFeign.getUser();
        orderDao.setOrderId("0001");
        orderDao.setOrderName("0001");
        orderDao.setUserId(user);
        return orderDao;
    }

    /**
     * 模拟调用randomUser时Feign超时 randomUser设定休眠1500
     * @return
     */
    @RequestMapping("/randomOrder")
    public OrderDao randomOrder() {
        OrderDao orderDao = new OrderDao();
        String user = userFeign.randomUser();
        orderDao.setOrderId("0001");
        orderDao.setOrderName("0001");
        orderDao.setUserId(user);
        return orderDao;
    }

    /**
     * 开启服务降级，解决雪崩效应的接口
     * HystrixCommand默认启动了服务降级 服务熔断 服务隔离服务隔离（每个接口在tomcat中都是一个独立的线程池）
     * 当达到本服务阈值后，使用getOrdersByUserNameFallbackMethod方法返回
     * 可以通过对比orderInfo的访问查看效果
     * @param userName
     * @return
     */
    @HystrixCommand(fallbackMethod = "getOrdersByUserNameFallbackMethod")
    @RequestMapping("/getOrdersByUserNameForHystrix")
    @Override
    public OrderDao getOrdersByUserNameForHystrix(String userName) {
        System.out.println("服务正常，线程号："+Thread.currentThread().getName());
        return this.getOrdersByUserName(userName);
    }

    /**
     * 开启服务降级，解决雪崩效应的接口,并通过回掉类的方式声明服务降级、熔断后的策略
     * HystrixCommand默认启动了服务降级 服务熔断 服务隔离服务隔离（每个接口在tomcat中都是一个独立的线程池）
     * 可以通过对比orderInfo的访问查看效果
     * @param userName
     * @return
     */
    @HystrixCommand
    @RequestMapping("/getOrdersByUserNameForHystrixs")
    @Override
    public OrderDao getOrdersByUserNameForHystrixs(String userName) {
        System.out.println("服务正常，线程号："+Thread.currentThread().getName());
        OrderDao orderDao = new OrderDao();
        String user = userFallBackFeign.getUser();
        orderDao.setUserId(user);
        orderDao.setOrderId("0001");
        orderDao.setOrderName("0001");
        return orderDao;
    }

    /**
     * 开启服务降级，解决雪崩效应的接口
     * 模拟调用randomUser时Feign超时 randomUser设定休眠1500
     * HystrixCommand默认熔断时间为1000，需要设置禁止超时时间，否则调用的逻辑走的是服务降级方法，之外的代码执行无影响
     * @return
     */
    @HystrixCommand(fallbackMethod = "getOrdersByUserNameFallbackMethod")
    @RequestMapping("/randomOrderForHystrix")
    @Override
    public OrderDao randomOrderForHystrix() {
        System.out.println("服务正常，线程号："+Thread.currentThread().getName());
        return this.randomOrder();
    }

    /**
     * 测试服务状态
     * @return
     */
    @RequestMapping("/orderInfo")
    @Override
    public String orderInfo() {
        String result = "服务正常，线程号："+Thread.currentThread().getName();
        System.out.println(result);
        return result;
    }

    /**
     * 正常服务降级后应执行的方法
     * @return
     */
    public OrderDao getOrdersByUserNameFallbackMethod() {
        System.out.println("服务器正忙，请稍后重试");
        return null;
    }
}
